package cn.fanqinghong.ymcc.mq.consumer;

import cn.fanqinghong.ymcc.constants.AliPayConstants;
import cn.fanqinghong.ymcc.dto.NotifyDto;
import cn.fanqinghong.ymcc.service.ICourseService;
import cn.fanqinghong.ymcc.service.ICourseUserLearnService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.spring.annotation.MessageModel;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * FAN
 * 2022/8/2  10:11
 */
@RocketMQMessageListener(
        consumerGroup = AliPayConstants.COURSE_ALI_ORDER_NOTIFY_GROUP,
        topic = AliPayConstants.ALI_ORDER_NOTIFY_TOPIC,
        selectorExpression = AliPayConstants.ALI_ORDER_NOTIFY_TAG,
        messageModel = MessageModel.BROADCASTING
)
@Component
@Slf4j
public class AlipayCourse2CourseOrderCourseConsumer implements RocketMQListener<MessageExt> {
    @Autowired
    private ICourseUserLearnService courseUserLearnService;
    @Override
    public void onMessage(MessageExt messageExt) {
        log.info("课程微服务，处理订单消息");
        byte[] body = messageExt.getBody();
        if (body == null || body.length == 0){
            return;//消息不对直接签收
        }
        String msgStr = new String(body, StandardCharsets.UTF_8);
        NotifyDto notifyDto = JSONObject.parseObject(msgStr, NotifyDto.class);
        courseUserLearnService.saveUserLearn(notifyDto);
        }

}
